package com.qiqidream.admin.system.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiqidream.admin.common.base.BaseController;
import com.qiqidream.admin.common.entity.AuthJson;
import com.qiqidream.admin.common.entity.PageEntity;
import com.qiqidream.admin.common.enums.MenuType;
import com.qiqidream.admin.common.utils.tree.TreeUtil;
import com.qiqidream.admin.system.model.dto.RoleDto;
import com.qiqidream.admin.system.model.dto.UserDto;
import com.qiqidream.admin.system.model.entity.Menu;
import com.qiqidream.admin.system.model.entity.Role;
import com.qiqidream.admin.system.model.entity.User;
import com.qiqidream.admin.system.model.vo.MenuVo;
import com.qiqidream.admin.system.model.vo.RoleVo;
import com.qiqidream.admin.system.model.vo.UserInfoVo;
import com.qiqidream.admin.system.model.vo.UserVo;
import com.qiqidream.admin.system.service.MenuService;
import com.qiqidream.admin.system.service.RoleService;
import com.qiqidream.admin.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * 首页
 * @author QiQiDream
 * @since 2019/11/18 10:53
 */
@RestController
@Api(tags = "角色-接口")
@RequestMapping("/system/role")
public class RoleController extends BaseController {
    @Resource
    RoleService roleService;

    @ApiOperation(value = "查询角色列表")
    @GetMapping("")
    public PageEntity<RoleVo> getRoles(@RequestParam(value = "page", defaultValue = "1") Long page,
                                       @RequestParam(value = "count", defaultValue = "10") Long count,
                                       @RequestParam(value = "name",required = false) String name) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.and(query -> query.like(Role::getName,name).or().like(Role::getRemark,name));
        }
        IPage<Role> iPage = roleService.page(new Page<>(page, count),wrapper);
        return new PageEntity<>(iPage.getTotal(),Convert.convert(new TypeReference<List<RoleVo>>() {}, iPage.getRecords()));
    }

    @ApiOperation(value = "获取角色信息")
    @GetMapping("/{id}")
    public RoleVo getRole(@PathVariable Long id) {
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(roleService.getById(id),roleVo);
        return roleVo;
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("")
    public Boolean addRole(@Valid @RequestBody RoleDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        roleService.save(role);
        return true;
    }

    @ApiOperation(value = "编辑角色")
    @PutMapping("")
    public Boolean editRole(@RequestBody RoleDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        roleService.updateById(role);
        return true;
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/{id}")
    public Boolean removeRole(@PathVariable Long id) {
        roleService.removeById(id);
        return true;
    }

    @ApiOperation(value = "设置角色菜单")
    @PutMapping("/{id}/menu")
    public Boolean editRoleMenus(@RequestBody List<Long> menuIds, @PathVariable Long id) {
        AuthJson authJson = new AuthJson();
        authJson.setMenu(menuIds);

        Role role = new Role();
        role.setId(id);
        role.setAuthJson(authJson);

        roleService.updateById(role);
        return true;
    }


}
